package com.codelathe.gwt.client;

import com.google.gwt.core.client.JavaScriptObject;

public class SoundManager{
	private static SoundManager m_smobject = null;

	
	private SoundManager()
	{
	}
	
	public static SoundManager createInstance()
	{
		if(m_smobject == null)
		{
			m_smobject  = new SoundManager();
			m_smobject.init();
		}
		return m_smobject;
	}
/**
 * Sound Manager Property Functions 
 */	
	
	/**
     * Sets the polling mode for SoundManager
     * @param mode true to enable polling mode <br>
     * Default: true
     */
	public static native void setPolling(boolean mode)/*-{
		var smobject = $wnd.soundManager;
		smobject.allowPolling = mode;
	}-*/;

	/**
     * Returns whether polling is enabled for SoundManager.
     * @return true if polling mode is enabled SoundManager.
     */
	public static native boolean getPolling()/*-{
		var smobject = $wnd.soundManager;
		return smobject.allowPolling;
	}-*/;

	
	/**
     * Sets the debug mode for SoundManager
     * Output written into (div#soundmanager-debug, OR console if available + configured)
     * @param mode true to enable debug mode <br>
     * Default: true
     */
	public static native void setDebugMode(boolean mode)/*-{
		var smobject = $wnd.soundManager;
		smobject.debugMode = mode;
	}-*/;

	/**
     * Returns whether debug mode has been enabled for SoundManager.
     * @return true if debug mode is enabled SoundManager.
     */
	public static native boolean getDebugMode()/*-{
		var smobject = $wnd.soundManager;
		return smobject.debugMode;
	}-*/;

	
	/**
     * Disables debug console toggle handle on the html page 
     */
	public static native void disableDebugToggle()/*-{
		var smobject = $wnd.soundManager;
		smobject.debugID = null;
	}-*/;
	
	/**
     * Enables/disables console output for SoundManager
     * @param mode true to enable console mode <br>
     * Default: true
     */
	public static native void setConsoleMode(boolean mode)/*-{
		var smobject = $wnd.soundManager;
		smobject.useConsole = mode;
	}-*/;

	/**
     * Returns whether console mode output has been enabled for SoundManager.
     * @return true if console mode output is enabled SoundManager.
     */
	public static native boolean getConsoleMode()/*-{
		var smobject = $wnd.soundManager;
		return smobject.useConsole;
	}-*/;
	
	/**
     * Enables/disables console only output for SoundManager
     * @param mode true to enable console only output. <br>
     * Default: false 
     */
	public static native void setConsoleOnlyMode(boolean mode)/*-{
		var smobject = $wnd.soundManager;
		smobject.consoleOnly = mode;
	}-*/;

	/**
     * Returns whether console only mode has been enabled for SoundManager.
     * @return true if console only mode is enabled SoundManager.
     */
	public static native boolean getConsoleOnlyMode()/*-{
		var smobject = $wnd.soundManager;
		return smobject.consoleOnly;
	}-*/;
	
	/**
     * Sets the null url for the SoundManager
     * Path to "null" (empty) MP3 file, used to unload sounds (Flash 8 only)
     * @param url url string to null mp3 file. <br>
     * Default: 'data/null.mp3'  
     */
	public static native void setNullURL(String url)/*-{
		var smobject = $wnd.soundManager;
		smobject.nullURL = url;
	}-*/;

	/**
     * Returns null url for the SoundManager.
     * @return String URL of null mp3 for the SoundManager.
     */
	public static native String getNullURL()/*-{
		var smobject = $wnd.soundManager;
		return smobject.nullURL;
	}-*/;
	
	/**
     * Sets the url for the SoundManager,
     * Path to SWF file.
     * @param url url string to SoundManager SWF file. <br>
     * Default: './SoundManager2.swf'  
     */
	public static native void setSoundManagerURL(String url)/*-{
		var smobject = $wnd.soundManager;
		smobject.url = url;
	}-*/;

	/**
     * Returns null url for the SoundManager.
     * @return String URL of null mp3 for the SoundManager.
     */
	public static native String getSoundManagerURL()/*-{
		var smobject = $wnd.soundManager;
		return smobject.url;
	}-*/;

	/**
     * Enables/disables auto loading of a sound.
     * otherwise load() function has to be called
     * explicitly before play() can be called
     * @param auto true to enable sound auto loading. <br>
     * Default: false 
     */
	public static native void setAutoLoad(boolean auto)/*-{
		var smobject = $wnd.soundManager;
		smobject.defaultOptions.autoLoad = auto;
	}-*/;

	/**
     * Returns whether sound auto loading has been enabled for SoundManager.
     * @return true if auto load is enabled for SoundManager.
     */
	public static native boolean getAutoLoad()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.autoLoad;
	}-*/;
	
	/**
     * Enables/disables streaming of sound.
     * Allows playing before entire file has loaded (recommended)
     * @param mode true to enable streaming. <br>
     * Default: true
     */
	public static native void setStreamMode(boolean mode)/*-{
		var smobject = $wnd.soundManager;
		smobject.defaultOptions.stream = mode;
	}-*/;

	/**
     * Returns whether streaming has been enabled for SoundManager.
     * @return true if streaming is enabled for SoundManager.
     */
	public static native boolean getStreamMode()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.stream;
	}-*/;
	
	/**
     * Enables/disables auto playing of a sound.
     * enable playing of file as soon as possible
     * @param auto true to enable sound auto playing. <br>
     * Default: false 
     */
	public static native void setAutoPlay(boolean auto)/*-{
		var smobject = $wnd.soundManager;
		smobject.defaultOptions.autoPlay = auto;
	}-*/;

	/**
     * Returns whether sound auto playing has been enabled for SoundManager.
     * @return true if auto play is enabled for SoundManager.
     */
	public static native boolean getAutoPlay()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.autoPlay;
	}-*/;
	
	/**
     * Enables/disables multi shot playing of a sound,
     * allowing sound restart or layer on top of each other 
     * when played multiple times, rather than one-shot/one at a time
     * @param multi true to enable multi shot playing. 
     */
	public static native void setMultiShot(boolean multi)/*-{
		var smobject = $wnd.soundManager;
		smobject.defaultOptions.multiShot = multi;
	}-*/;

	/**
     * Returns whether multi shot playing has been enabled for SoundManager.
     * @return true if multi-shot is enabled for SoundManager.
     */
	public static native boolean getMultiShot()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.multiShot;
	}-*/;

	/**
     * Set the left/right sound balance.
     * settings range from left(-100) to right(100). 
     * @param balance int value ranging between -100 to 100 <br>
     * Default: 0
     */
	public static native void setBalance(int balance)/*-{
		var smobject = $wnd.soundManager;
		smobject.setPan(balance);
	}-*/;

	/**
     * Returns left-right balance of SoundManager
     * @return int ranging from -100 for left to +100 for right.
     */
	public static native int getBalance()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.pan;
	}-*/;
	
	/**
     * Set the volume level.
     * settings range from minimum (0) to maximum(100). 
     * @param volume int value ranging between 0 to 100 <br>
     * Default: 100
     */
	public static native void setVolume(int volume)/*-{
		var smobject = $wnd.soundManager;
		smobject.setVolume(volume);
	}-*/;

	/**
     * Returns volume level for SoundManager
     * @return int ranging from 0 for min to +100 for max.
     */
	public static native int getVolume()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.volume;
	}-*/;
	
	/**
     * Set milliseconds before which onBeforeFinish should be fired.
     * @param msec milliseconds before the event should be fired <br>
     * Default: 5000
     */
	public static native void setBeforeFinishTime(int msec)/*-{
		var smobject = $wnd.soundManager;
		msec = (msec < 0 )?0:msec;
		smobject.defaultOptions.onbeforefinishtime = msec;
	}-*/;

	/**
     * Returns the time in milliseconds before the sound/song finishes  
     * @return  milli seconds before finish.
     */
	public static native int getBeforeFinishTime()/*-{
		var smobject = $wnd.soundManager;
		return smobject.defaultOptions.volume;
	}-*/;
	
/**
 * End of Sound Manager Property Functions 
 */	

	/**
     * Sets SoundManager Listener.
     * There can be only one listener set a time in a JVM session.
     * @param listener the listener
     */
	public static native void setSoundManagerListener(SoundManagerListener listener)/*-{
		var smobject = $wnd.soundManager;
		smobject.onerror = function() {		
		 listener.@com.codelathe.gwt.client.SoundManagerListener::onSoundManagerError()();
		}

		smobject.onload = function() {		
		 listener.@com.codelathe.gwt.client.SoundManagerListener::onSoundManagerLoad()();
		}
	}-*/;
	
	/**
     * Sets Sound Listener.
     * There can be only one listener set a time in a JVM session.
     * @param listener the listener
     */
    public native void setSoundListener(SoundListener listener)/*-{
    	var smobject = $wnd.soundManager;
    	
    	smobject.defaultOptions.whileplaying = function() {
		 listener.@com.codelathe.gwt.client.SoundListener::whilePlaying()();
		}
		
		smobject.defaultOptions.whileloading = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::whileLoading()();
		}
		
		smobject.defaultOptions.onload = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onLoadComplete()();
		}
		

		smobject.defaultOptions.onplay = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onPlayStart()();
		}

		smobject.defaultOptions.onstop = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onPlayStop()();
		}

		smobject.defaultOptions.onpause = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onPause()();
		}

		smobject.defaultOptions.onresume = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onResume()();
		}

		smobject.defaultOptions.onfinish = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onFinish()();
		}
		
		smobject.defaultOptions.onbeforefinish = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onBeforeFinish()();
		}

		smobject.defaultOptions.onbeforefinishcomplete = function() {		
		 listener.@com.codelathe.gwt.client.SoundListener::onBeforeFinishComplete()();
		}

    }-*/;
    
    /**
     * Unregister SoundManager Listener.
     */
    public native void unsetSoundManagerListener()/*-{
    	var smobject = $wnd.soundManager;
    	smobject.defaultOptions.whileplaying = null;
		smobject.defaultOptions.whileloading = null;
		smobject.defaultOptions.onload = null;
		smobject.defaultOptions.onplay = null;
		smobject.defaultOptions.onstop = null;
		smobject.defaultOptions.onpause = null;
		smobject.defaultOptions.onresume = null;
		smobject.defaultOptions.onfinish = null;
		smobject.defaultOptions.onbeforefinish = null;
		smobject.defaultOptions.onbeforefinishcomplete = null;

    }-*/;
    
    private native void init() /*-{
		var smobject = $wnd.soundManager;
		smobject.init();
	}-*/;
    
    /**
     * Gets the sound object for the givenID
     * @param id identifier for the song
     * @param url url for the mp3 song
     */
    public Sound getSoundById(String id)
    {
    	Sound s = new Sound(getJSSoundById(id));
    	return s;
    }
    
    private native JavaScriptObject getJSSoundById(String id)/*-{
		var smobject = $wnd.soundManager;
		return smobject.getSoundById(id);
    }-*/;
    
    /**
     * Creates a sound object with a given url
     * @param id identifier for the song
     * @param url url for the mp3 song
     */
    public Sound createSound(String id, String url)
    {
    	Sound s = new Sound(createJSSound(id,url));
    	return s;
    }
    
    private native JavaScriptObject createJSSound(String id, String url)/*-{
		var smobject = $wnd.soundManager;
		return smobject.createSound(id,url);
    }-*/;
    
    /**
     * Creates and plays a sound object with a given url
     * @param id identifier of the song
     * @param url url of the song
     */
    public native void play(String id, String url)/*-{
    	var smobject = $wnd.soundManager;
		smobject.play(id,url);
    }-*/;

    /**
     * Stops the song/sound with the given identifier
     * @param id identifier of the song
     */
    public native void stop(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.stop(id);
    }-*/;

    /**
     * Pauses the song/sound with the given identifier
     * @param id identifier of the song
     */
    public native void pause(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.pause(id);
    }-*/;
    
    /**
     * Resume the song/sound with the given identifier
     * @param id identifier of the song
     */
    public native void resume(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.pause(id);
    }-*/;

    /**
     * Destroys sound object with a given identifier
     * @param id identifier of the song
     */
    public native void destroySound(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.destroySound(id);
    }-*/;
    
    /**
     * Toggles sound object with a given identifier
     * @param id identifier of the song
     */
    public native void togglePause(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.togglePause(id);
    }-*/;
    
    /**
     * Toggles sound object with a given identifier
     * @param id identifier of the song
     * @param volume level of the song
     */
    public native void setVolume(String id, int vol)/*-{
    	var smobject = $wnd.soundManager;
		smobject.setVolume(id, vol);
    }-*/;

    /**
     * Sets the balance of sound object with a given identifier
     * @param id identifier of the song
     * @param volume level of the song
     */
    public native void setBalance(String id, int balance)/*-{
    	var smobject = $wnd.soundManager;
		smobject.setPan(id, balance);
    }-*/;
    
    /**
     * Sets the position of play for the sound object with a given identifier
     * @param id identifier of the song
     * @param milli sec level of the song
     */
    public native void setPosition(String id, int msec)/*-{
    	var smobject = $wnd.soundManager;
		smobject.setPosition(id, msec);
    }-*/;
    
    /**
     * Mute the song/sound with the given identifier
     * @param id identifier of the song
     */
    public native void mute(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.mute(id);
    }-*/;
    
    /**
     * UnMutes the song/sound with the given identifier
     * @param id identifier of the song
     */
    public native void unmute(String id)/*-{
    	var smobject = $wnd.soundManager;
		smobject.unmute(id);
    }-*/;


}